<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 15, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class DbLanguage {
	/**
	 * Array lable with struct: $domain=>$labelKey=>$label
	 * @var array
	 */
	var $_labelArray = array();
	
	/**
	 * The language of this object
	 * @var string
	 */
	var $_lang;
	
	/**
	 * The database for storing language labels
	 * @var Database
	 */
	var $_db;
	
	/**
	 * Constructor
	 * @param $language string 'en/vi/....'
	 * @param $db Database database for label table
	 */
	function __construct($language=null, &$db=null) {
		if (!$db) {
			$db = &SysDatabase::getInstance();
		}
		if (!$language) {
			$language = SystemSite::getLanguage();
		}
		
		$this->_db = $db;
		
		// load
		$sql = "SELECT `domain`, `key`, `label_{$language}` FROM label_key";
		$db->setQuery($sql);
		$cur = $db->query();

		if (!$cur) {
			return;
		}
		
		$lblArray = &$this->_labelArray;
		while ( $row = mysql_fetch_row ( $cur ) ) {
			$lblArray[$row[0]][$row[1]] = $row[2];
		}
		mysql_free_result ( $cur );
	}
	
	/**
	 * Function get the label of a key with current language.
	 * NOTE: if the label don't exists this function will try 
	 * to insert the new key label row in the database
	 * 
	 * @param $keyLabel string
	 * @param $domain string this can be: 'site','list', 'tip'....
	 * @return string
	 */
	function getLabel($keyLabel, $domain='site') {
		if (empty($keyLabel)) {
			return;
		}
		
		$lblArray = &$this->_labelArray;
		$arr = &$lblArray[$domain];
		if ($lblArray[$domain][$keyLabel]) {
			return $lblArray[$domain][$keyLabel];
		}
		
		// if don't exists then we insert the new one into the database
		RClassesImporter::cls('dbo.dtos.LabelKeyDTO');
		$dto = new LabelKeyDTO();
		if (get_magic_quotes_gpc ()) {
			$dto->key = addslashes($keyLabel);
			$dto->setAllLable(addslashes($keyLabel));
			$dto->domain = addslashes($domain);
		}
		else {
			$dto->domain = $domain;
			$dto->key = $keyLabel;
			$dto->setAllLable($keyLabel);
		}
		
		$sql = $dto->createInsertSQL();
		$db = $this->_db;
		$db->setQuery($sql);
		$db->query();
		
		$lblArray[$domain][$keyLabel] = $keyLabel;
		
		return $keyLabel;
	}
}


class SysDBLanguage {
	
	/**
	 * Get the system dblang instance
	 * @return DbLanguage
	 */
	static function &getInstance() {
		static $inst;
		if ($inst) {
			return $inst;
		}
		$inst = new DbLanguage();
		return $inst;
	}
	
	/**
	 * Get the label
	 * @param $key
	 * @param $domain
	 * @return string label
	 */
	static function _($key, $domain=null) {
		if (empty($domain)) {
			$domain = SysRequest::application();
		}
		$dbLang = &self::getInstance();
		return $dbLang->getLabel($key, $domain);
	}
	
	/**
	 * Echo the translated string.
	 *
	 * @param string $keyWord
	 */
	static function printString($key, $domain=null) {
		echo self::_($key, $domain);
	}
}